package com.authority.framework.web.controller;

import com.authority.framework.log.Log;
import com.authority.framework.log.enums.BusinessType;
import com.authority.framework.web.domain.*;
import com.authority.framework.web.service.SysLoginInForService;
import com.authority.framework.base.util.laydata.LayDatas;
import com.authority.framework.base.util.laydata.PageUtils;
import com.authority.framework.base.util.laydata.ResultModel;
import com.authority.framework.base.util.laydata.ResultTools;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * @program: authority
 * @description: 登录日志控制层
 * @author: Mr.Yan
 * @create: 2018-11-20 18:01
 **/
@Controller
@RequestMapping(value = "syslogininforCon")
public class SysLoginInForController {

	private static final Logger log = LoggerFactory.getLogger(SysLoginInForController.class);

	@Autowired
	private SysLoginInForService sysLoginInForService;

	private Map<String, Object> paramMap;//参数

	private Map<String, Object> resultMap;//返回值

	/**
	 * @Description: 获取登录日志展示数据
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/15
	 */
	@RequestMapping("/list")
	@ResponseBody
	public LayDatas list(HttpServletRequest request, HttpServletResponse response,
			PageUtils pageUtils) {
		return sysLoginInForService.pageLayInfo("system/SysLoginInForSql", SysLoginInForEntity.class, pageUtils);
	}

	/**
	 * @Description: 删除多条登录日志
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/27
	 */

	@Log(title = "登录日志管理", businessType = BusinessType.DELETE)
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ResultModel delete(HttpServletRequest request, HttpServletResponse response,
			@RequestParam(name = "infoIds", required = false) String infoIds) {
		try {
			String[] infoIdArr = infoIds.split(",");
			for (String infoId : infoIdArr) {
				Integer update = sysLoginInForService.delete(SysLoginInForEntity.class, Integer.valueOf(infoId));
				if (update < 1) {
					return ResultTools.result(404, "删除登录日志信息失败", null);
				}
			}
			return ResultTools.result(0, "", null);
		}
		catch (Exception e) {
			log.error("删除登录日志信息出现未知错误" + e.getMessage());
			return ResultTools.result(404, "删除登录日志信息出现未知错误", null);
		}
	}

	/**
	 * @Description: 清空登录日志
	 * @Param:
	 * @return:
	 * @Author: Mr.Yan
	 * @Date: 2018/12/27
	 */

	@Log(title = "登录日志管理", businessType = BusinessType.CLEAN)
	@ResponseBody
	@RequestMapping(value = "/deleteAll")
	public ResultModel deleteAll(HttpServletRequest request, HttpServletResponse response) {
		try {
			sysLoginInForService.deleteAll();
			return ResultTools.result(0, "", null);
		}
		catch (Exception e) {
			log.error("删除登录日志信息出现未知错误" + e.getMessage());
			return ResultTools.result(404, "删除登录日志信息出现未知错误", null);
		}
	}

}
